/*********************************************************************************
 *      Copyright:  (C) 2020 YangZhengQing<2489833081@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  argument_parse.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(04/03/2020)
 *         Author:  YangZhengQing <2489833081@qq.com>
 *      ChangeLog:  1, Release initial version on "04/03/2020 08:02:33 PM"
 *                 
 ********************************************************************************/

#include"../include/client.h"

/* 
int main(int argc,char **argv)
{
    char                *serv_IP = NULL;//服务器IP
    int                 serv_port = 0;//服务器端口
    int                 rv = -1;//函数返回值

    rv = argument_parse(argc,argv,&serv_IP,&serv_port);//参数解析
    if(0 == rv)
    {
        printf("解析出服务器serv_IP为:%s\n",serv_IP);
//        printf("服务器端口为:%d\n",serv_port);
    }

    return 0;
}
*/

/*功能：打印帮助信息
 * 参数：order:执行的程序名
 * 返回值：无*/
static void print_usage(char *order)
{
    printf("%s usages:\n",order);
    printf("-I(IP):server IP\n");
    printf("-p(port):server port\n");
    printf("-d(domain):domain name prase\n");
    printf("-h(help):help information\n");

}

/* 功能：参数解析
 * 参数：argc：参数个数
 *       argv：保存参数的字符串指针
 *       serv_IP：服务器ip(output)
 *       serv_port：服务器端口(output)
 * 返回值：成功返回0，错误返回-1*/
int argument_parse(int argc,char **argv,char **serv_IP,int *serv_port)
{
    int                 optopt;//getopt返回值
    int                 domain_mark = 0;//域名解析标志
   
    //定义选项处理方案
    struct option       long_options[]=
    {
        {"IP",1,NULL,'i'},
        {"port",1,NULL,'p'},
        {"help",0,NULL,'h'},
        {0,0,0,0}

    };

    while((optopt=getopt_long(argc,argv,"i:p:d:h",long_options,NULL)) > 0)
    {

        switch(optopt)
        {


            case 'i':
                *serv_IP = optarg;
                break;
            case 'p':
                *serv_port = atoi(optarg);
                break;
            case 'd':
                *serv_IP = DNS(optarg);//域名解析
                domain_mark = 1;//表示进行了域名解析
 //               printf("serv_ip=%s\n",*serv_IP);
                break;
            case 'h':
                print_usage(argv[0]);
                break;
            default:
                break;

        }

    }

    if(!(*serv_IP)||(!serv_port && !domain_mark))
    {
        print_usage(argv[0]);
        return -1;
    }
    
    return 0;
}


